package com.jiushi.redis.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiushi.redis.entity.User;
import com.jiushi.redis.mapper.UserMapper;
import com.jiushi.redis.service.UserService;
import com.jiushi.redis.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService{

    @Override
    public Result login(User user) {
        // 1.数据校验
        String username = user.getUsername();
        String password = user.getPassword();
        // 2.根据用户名查询用户信息
        User u = lambdaQuery().eq(User::getUsername, username).one();
        if(u == null){
            return Result.error("用户名不存在！");
        }
        if(!u.getPassword().equals(password)){
            return Result.error("密码错误！");
        }
        return Result.ok("登录成功！");
    }

    @Override
    public Result register(User user) {
        save(user);
        return Result.ok("注册成功！");
    }

    @Cacheable(value = "user", key = "#id")
    @Override
    public User testAdd(Long id) {
        User user = lambdaQuery().eq(User::getId, id).one();
        log.info("查询到User:{}", user);
        return user;
    }

    @CacheEvict(value = "user", key = "#id")
    @Override
    public User testDelete(Long id) {
        User user = lambdaQuery().eq(User::getId, id).one();
        return user;
    }
}
